const fileService = require("../service/file.service");
const userService = require("../service/user.service");
const { SERVER_HOST, SERVER_PORT } = require("../config/server");
const BaseController = require("./BaseController");
class FileController extends BaseController {
  async create(ctx, next) {
    //1.获取文件信息
    const { filename, mimetype, size } = ctx.request.file;
    const { id } = ctx.user;
    //2.将文件信息和用户存储到数据库中
    const result = await fileService.create(filename, mimetype, size, id);
    //3.将头像的地址信息,保存在user表中
    const avatarUrl = `${SERVER_HOST}:${SERVER_PORT}/user/avatar/${id}`;
    const result2 = await userService.updateUserAvatar(avatarUrl, id);
    //4.返回结果
    ctx.body = {
      code: 0,
      msg: "上传成功",
      data: avatarUrl,
    };
  }
}

module.exports = new FileController(fileService);
